package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @package: com.cskaoyan.gateway.controller.shopping
 * @Description:
 * @author: 北青
 * @date: 2022/1/19 22:30
 */
@RestController
@RequestMapping("/shopping")
@Slf4j
public class ProductController {

    @Reference(timeout = 3000, check = false)
    IProductService service;

    /**
     * 查询推荐商品
     *
     * @return
     */
    @GetMapping("/recommend")
    @Anoymous
    public ResponseData recommends() {

        RecommendResponse recommendGoods = service.getRecommendGoods();
        //判断是否成功
        if (!recommendGoods.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            //说明没成功获取信息
            return new ResponseUtil().setErrorMsg(recommendGoods.getMsg());
        }
        return new ResponseUtil().setData(recommendGoods.getPanelContentItemDtos());
    }

    /**
     * 查看商品详情
     */
    @GetMapping("product/{id}")
    @Anoymous
    public ResponseData getProductDetail(@PathVariable("id") Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = service.getProductDetail(request);
        //判断是否成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            ResponseData responseData = new ResponseUtil().setData(response.getProductDetailDto());
            return responseData;
        }
        //说明没成功信息
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 分页查询商品列表
     */
    @GetMapping("goods")
    @Anoymous
    public ResponseData getProductListByPage(AllProductRequest request) {
        AllProductResponse response = service.getProductListByPage(request);
        //判断是否成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            ResponseData responseData = new ResponseUtil().setData(response.getAllProductResponseResult());
            return responseData;
        }
        //说明没成功信息
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
